package net.hubbery.dao;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import net.hubbery.bean.Bill;
import net.hubbery.bean.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class BillDao {

    private static SimpleDateFormat sdf=
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public boolean createBill(Bill bill) throws SQLException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String sql;
        sql = "insert into bill values(null,null,?,?,?,?,?);";
        int row = queryRunner.update(sql, bill.getRoomId(), bill.getCustomerName(), sdf.format(new Date()), null, null);
        System.out.println(sdf.format(new Date()));
        if(row>0){
            return true;
        }else {
            return false;
        }
    }
    //查找房间是不是被占用了，如果price的值不为空则被占用，返回false，不能预定
    public boolean checkRoom(String roomId) throws SQLException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String sql = "select price from bill where roomId = ?";
        Bill bill = queryRunner.query(sql, new BeanHandler<Bill>(Bill.class), roomId);
        if(bill == null){
            return true;
        }else {
            return false;
        }


    }

    public List<Bill> getBills() throws SQLException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String sql = "select * from bill";
        List<Bill> bills = queryRunner.query(sql,new BeanListHandler<Bill>(Bill.class));
        return bills;
    }
}
